import { config } from '../../../config';
import path from 'path';
import fs from 'fs';
import { sequelize } from '../../db';
import { Model, DataTypes } from 'sequelize';
import { cwd } from 'node:process';

class ImgRate extends Model { }

ImgRate.init({
    RateId: DataTypes.INTEGER,
    url: DataTypes.STRING,
    fullUrl: {
        type: DataTypes.VIRTUAL,
        get() {
            return this.dataValues.url ? config.BASEURL + this.dataValues.url : ''
        },
        set(value) {
            throw new Error('不要尝试设置 `fullUrl` 的值!');
        }
    },
    isImage: {
        type: DataTypes.BOOLEAN,
        defaultValue: true
    }
}, {
    hooks: {
        afterDestroy: (user, options) => {
            fs.unlink(cwd() + '/public/uploads/' + path.basename(user.previous().url), (err) => {
                // console.log(err)
            })
        }
    },
    createdAt: false,
    updatedAt: false,
    sequelize
});

export default ImgRate